package com.weirn.spring.commons.utils;

import com.alibaba.fastjson.JSONObject;
import com.weirn.spring.commons.constants.AppIdEnum;
import com.weirn.spring.commons.vo.ConfigVO;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.File;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by runing.wei on 2018/6/27.
 */
public class Dom4jUtils {

    @Test
    public static List<ConfigVO> readXMLDemo(String path) throws Exception {

//        String path = "/Users/yp-tc-2563/Downloads/_apps_data_tomcat-temp_20180612185510.xml";

        // 创建saxReader对象
        SAXReader reader = new SAXReader ();
        // 通过read方法读取一个文件 转换成Document对象
        Document document = reader.read(new File (path));
        //获取根节点元素对象
        Element node = document.getRootElement();
        List<ConfigVO> lists = new LinkedList<ConfigVO> (  );
        //遍历所有的元素节点
         listNodes(lists,node);
         for(ConfigVO vo : lists){

             if(vo.getAppId () == AppIdEnum.TZT.getId ()
                     || vo.getAppId () == AppIdEnum.JRCONFIG.getId ()){
                 System.out.println ( JSONObject.toJSONString ( vo ));
                 continue;
             }

             if(vo.getConfigKey ().contains ( "TZT" )
                     || vo.getConfigKey ().contains ( "tzt")){
                 System.out.println ( JSONObject.toJSONString ( vo ));
                 continue;
             }
         }
         return lists;
    }

    /**
     * 遍历当前节点元素下面的所有(元素的)子节点
     *
     * @param node
     */
    public static void listNodes(List<ConfigVO> configVOs,Element node) {
//        System.out.println("当前节点的名称：：" + node.getName());
        // 获取当前节点的所有属性节点
        List<Attribute> list = node.attributes();


        // 遍历属性节点
//        for (Attribute attr : list) {
//            System.out.println(attr.getText() + "-----" + attr.getName()
//                    + "---" + attr.getValue());
//        }
//
//        if (!(node.getTextTrim().equals(""))) {
//            System.out.println("文本内容：：：：" + node.getText());
//        }

        if("ConfigVO".equals ( node.getName () )){
            // 当前节点下面子节点迭代器
            Iterator<Element> it = node.elementIterator();
            Map<String,String> nodeMap = new HashMap<String,String> ( 2 );
            ConfigVO configVO = new ConfigVO ();
            // 遍历
            while (it.hasNext()) {
                // 获取某个子节点对象
                Element e = it.next();
                String value = e.getStringValue ();
                nodeMap.put ( e.getName (),value );
            }
            Field[] fields = configVO.getClass ().getDeclaredFields ();
            for(Field field : fields){
                field.setAccessible ( true );
                if(nodeMap.containsKey ( field.getName () )){
                    String type = field.getGenericType().getTypeName ();
                    try {
                        if(String.class.getName ().equals ( type )){
                            field.set ( configVO,nodeMap.get ( field.getName () ) );
                        }else if(  Integer.class.getName ().equals ( type ) || int.class.getName ().equals ( type )){
                            field.set ( configVO,Integer.valueOf ( nodeMap.get ( field.getName () ) ) );
                        }else if(Date.class.getName ().equals ( type )){
                            field.set ( configVO,getDate ( nodeMap.get ( field.getName () ) ) );
                        }else if(Boolean.class.getName ().equals ( type ) || boolean.class.getName ().equals ( type )){
                            field.set ( configVO,Boolean.valueOf ( nodeMap.get ( field.getName () ) ) );
                        }

                    } catch ( IllegalAccessException e ) {
                        e.printStackTrace ();
                    }
                }
            }
            configVOs.add ( configVO );
        }else{
            // 当前节点下面子节点迭代器
            Iterator<Element> it = node.elementIterator();
            // 遍历
            while (it.hasNext()) {
                // 获取某个子节点对象
                Element e = it.next();
                // 对子节点进行遍历
                listNodes(configVOs,e);
            }
        }
    }

    private static Date getDate(String value){
        return new Date(value);
    }

    @Test
    public static void testGetDate(){
        String str = "Wed May 23 14:37:04 CST 2018";
       Date now =  getDate(str);
       System.out.println (now);
    }

}
